<?php namespace Common\Model;

use Hdphp\Model\Model;

class Goods extends Model
{
	//操作数据表
	protected $table= "goods";
	//自动验证
	protected $validate = array(
		array('gname','required','商品名称不能为空',3,3),
		array('gnumber','required','商品货号不得为空',3,3),
		array('marketprice','required','市场价格不能为空',3,3),
		array('shopprice','required','商场价格不能为空',3,3),
	);
	//自动完成
	//array(表单字段名,处理方法,方法类型,验证条件3为必须验证,处理时间1表示插入时验证,2为更新时验证)
	protected $auto = array(
		array('time','time','function',3,1),
		//自己定义处理方法，method表示方法
		array('admin_aid','userUid','method',3,1),
		//array()
	);
	/**
	 * 自动完成，自定义方法
	 * 为了把admin_aid 自动提交到数据库对应的字段
	 */
	public function userUid()
	{
		return $_SESSION['aid'];
	}
	/**
	 * 自动完成image方法
	 */
	public function image()
	{
		//设置上传路径
		$path = 'Upload/goods/'.date('ymd');
		//验证上传文件
		$files = Upload::type('jpg,png,gif')->path($path)->size(2000000)->make();
		//如果有文件上传
		if($files){
			//缩略
			//参数：上传过来的图片路径|存储路径|缩略宽|缩略高|缩略方式
			$thumbPath = Image::thumb($files[0]['path'],$path.'/thumb_'.$files[0]['basename'],200,200,5);
		}else{
			return "";
		}
	}
	
	/**
	 * 添加商品
	 */
	public function store()
	{
		if(empty($_POST['photo']))
		{
			$this->error = "未选择商品图册";
			return false;
		}
		if(empty($_POST['attr']))
		{
			$this->error = "未选择商品属性";
			return false;
		}
		if(empty($_POST['spec']))
		{
			$this->error = "未选择商品规格";
		}
		
		//接受add页面异步上传商品图册的数据，photo是input隐藏域里的name属性
		$photo = $_POST['photo'];
		//p($photo);//[0] => Upload/goods/160602/63381464866403.jpg
		//自定义空变量
		$small = "";
		$medium = "";
		$big = "";
		//将上传的同一张图片循环缩略成三种规格
		foreach ($photo as $k => $v) {
			//截取后缀
			$v1 = ltrim(strrchr($v,'/'),'/');
			//p($v1);//63381464866403.jpg
			//缩略
			//参数：上传过来的图片路径|存储路径|缩略宽|缩略高|缩略方式
			$small .=Image::thumb($v,'Upload/goods_thumb/'.date('ymd').'/'.'small_'.$v1,48,48,5).'|';
			$medium .=Image::thumb($v,'Upload/goods_thumb/'.date('ymd').'/'.'medium_'.$v1,68,68,5)."|";
			$big .=Image::thumb($v,'Upload/goods_thumb/'.date('ymd').'/'.'big_'.$v1,450,675,5)."|";
		}
		//p($small);p($medium);p($big);
		//去掉最右边的  | 线
		$big = rtrim($big,'|');
		$small = rtrim($small,'|');
		$medium = rtrim($medium,'|');
		//p($small);p($medium);p($big);die;
		//自动验证
		if(!$this->create()) return false;
		
		//实例化商品详情表
		$detailData = new \Common\Model\Detail;
		if(!$detailData->create()){
			$this->error = $detailData->getError();
			return false;
		}
		//处理添加type_tid字段
		//获得goods表里的category)cid字段
		$category_cid = $_POST['category_cid'];
		//实例化分类表的模型
		$cate = new \Common\Model\Category;		
		
		
		//获取分类表里数据即category_tid字段数据
		$cateData = $cate->where('cid','=',$category_cid)->first();
		//p($cateData);
		
		//把分类表里的category_tid字段赋给goods,用type_tid字段对应接受
		$this->data['type_tid'] = $cateData['category_tid'];
		//执行商品表添加
		//p($this->data);die;
		//自动返回自增的id
		$gid = $this->add();
		//p($gid);
		//2.执行商品详细表添加
		$detailData->data['goods_gid'] = $gid;
		$detailData->data['small'] = $small;
		$detailData->data['medium'] = $medium;
		$detailData->data['big'] = $big;

		$detailData->add();
		//3.执行商品属性表添加
		//实例化商品属性表
		$goodsAttrData = new \Common\Model\GoodsAttr;
		//p($goodsAttrData->get());die;
		
		//循环商品属性表属性值
		foreach (Q('post.attr',array()) as $k => $v) {
			$data = array(
				//属性值
				'gtvalue'=>$v,
				//附加价格
				'added'=>0,
				//商品ID
				'goods_gid'=>$gid,
				//属性id
				'typeattr_taid'=>$k,
			);
			$goodsAttrData->add($data);
		}
		foreach (Q('post.spec',array()) as $k => $v) {
			foreach ($v['color'] as $key => $value) {
				$data = array(
					//属性值
					'gtvalue'=>$value,
					//附加价格
					'added'=>$v['added'][$key],
					//商品id
					'goods_gid'=>$gid,
					//属性id
					'typeattr_taid'=>$k,
				);
				$goodsAttrData->add($data);
			}
			
		}
		return true;
	}
	
	/**
	 * 编辑
	 */
	public function edit($gid)
	{

		//自动验证
		if(!$this->create()) return false;
		//1.对商品表执行修改
		$this->save();//加隐藏域		
		/**
		 * 2.对商品详细表进行修改
		 */
		
		//实例化商品详细表
		$goodsDetail = new \Common\Model\Detail;
		//自动验证
		if(!$goodsDetail->create()) {
			$this->error = $goodsDetail->getError();
			return false;
		}
		//判断原图片是否存在
		if(!isset($_POST['photo']))
		{
			$this->error = "请选择上传的商品图册";
			return false;
		}
		
		//接受add页面异步上传商品图册的数据，photo是input隐藏域里的name属性
		$photo = $_POST['photo'];
		//p($photo);//[0] => Upload/goods/160602/63381464866403.jpg
		//自定义空变量
		$small = "";
		$medium = "";
		$big = "";
		//将上传的同一张图片循环缩略成三种规格
		foreach ($photo as $k => $v) {
			//截取后缀
			$v1 = ltrim(strrchr($v,'/'),'/');
			//p($v1);//63381464866403.jpg
			//缩略
			//参数：上传过来的图片路径|存储路径|缩略宽|缩略高|缩略方式
			$small .=Image::thumb($v,'Upload/goods_thumb/'.date('ymd').'/'.'small_'.$v1,48,48,5).'|';
			$medium .=Image::thumb($v,'Upload/goods_thumb/'.date('ymd').'/'.'medium_'.$v1,68,68,5)."|";
			$big .=Image::thumb($v,'Upload/goods_thumb/'.date('ymd').'/'.'big_'.$v1,450,675,5)."|";
		}
		//p($small);p($medium);p($big);
		//去掉最右边的  | 线
		$big = rtrim($big,'|');
		$small = rtrim($small,'|');
		$medium = rtrim($medium,'|');
		//p($small);p($medium);p($big);die;
		//定义一个商品属性空数组，把对应数据压进去
		$goods_attrData = array(
			'small'=>$small,
			'medium'=>$medium,
			'big'=>$big,
			'goods_gid'=>$gid,
			'intro'=>$_POST['intro'],
			'service'=>$_POST['service'],
		);
		
		//执行修改
		$goodsDetail->where('goods_gid','=',$gid)->update($goods_attrData);
		
		/**
		 * 3.处理商品属性表
		 */
		//实例化商品属性表
		$goodsAttrData = new \Common\Model\GoodsAttr;
		//p($goodsAttrData->get());die;
		//先删除,再添加
		$goodsAttrData->where('goods_gid','=',$gid)->delete();
		//循环商品属性表属性值并添加
		foreach (Q('post.attr',array()) as $k => $v) {
			$data = array(
				//属性值
				'gtvalue'=>$v,
				//附加价格
				'added'=>0,
				//商品ID
				'goods_gid'=>$gid,
				//属性id
				'typeattr_taid'=>$k,
			);
			$goodsAttrData->add($data);
		}
		foreach (Q('post.spec',array()) as $k => $v) {
			foreach ($v['color'] as $key => $value) {
				$data = array(
					//属性值
					'gtvalue'=>$value,
					//附加价格
					'added'=>$v['added'][$key],
					//商品id
					'goods_gid'=>$gid,
					//属性id
					'typeattr_taid'=>$k,
				);
				$goodsAttrData->add($data);
			}
			
		}

		return true;
	}
	/**
	 * 删除
	 */
	public function del($gid)
	{
		//删除商品表中的数据
		$this->where('gid','=',$gid)->delete();
		//删除商品属性表中的数据
		 	//先实例化，再删除
			$goodsAttr = new \Common\Model\GoodsAttr;
		 	//删除对应id数据
		 	$goodsAttr->where('goods_gid','=',$gid)->delete();
		//删除 商品详细表中的数据
			//先实例化，再删除
			$detail = new \Common\Model\Detail;
			//删除对应id数据
			$detail->where('goods_gid','=',$gid)->delete();
		return true;
		 
	}
}
